﻿using System;
using System.Collections.Generic;
using AccountModule;
using Framework;

namespace RecipeModule
{
    public class RecipeManager : Singleton<RecipeManager>
    {
        private int _recipeId = 1;
        
        public event Action RecipeUpdateEvent;
        
        private Dictionary<string, List<Recipe>> _mapName2recipes = new Dictionary<string, List<Recipe>>();

        public Recipe CreateEmptyRecipe()
        {
            Recipe recipe = new Recipe(_recipeId++);
            return recipe;
        }

        public Recipe CloneRecipe(int id, string name = null)
        {
            Recipe recipe = GetRecipe(id, name);
            return recipe.Clone();
        }
        
        public void AddRecipe(Recipe recipe, string name = null)
        {
            if (name == null)
            {
                name = AccountManager.instance.accountName;
            }   
            List<Recipe> listRecipe;
            if (!_mapName2recipes.TryGetValue(name, out listRecipe))
            {
                listRecipe = new List<Recipe>();
                _mapName2recipes.Add(name, listRecipe);
            }
            listRecipe.Add(recipe);

            if (RecipeUpdateEvent != null)
            {
                RecipeUpdateEvent();
            }
        }

        public void RemoveRecipeById(int recipeId, string name = null)
        {
            if (name == null)
            {
                name = AccountManager.instance.accountName;
            }   
            List<Recipe> listRecipe;
            if (_mapName2recipes.TryGetValue(name, out listRecipe))
            {
                for (var i = 0; i < listRecipe.Count; i++)
                {
                    if (listRecipe[i].id == recipeId)
                    {
                        listRecipe.RemoveAt(i);
                        break;
                    }
                }
                if (RecipeUpdateEvent != null)
                {
                    RecipeUpdateEvent();
                }
            }
        }

        public void ReplaceRecipe(Recipe recipe, string name = null)
        {
            if (name == null)
            {
                name = AccountManager.instance.accountName;
            }   
            List<Recipe> listRecipe;
            if (_mapName2recipes.TryGetValue(name, out listRecipe))
            {
                for (var i = 0; i < listRecipe.Count; i++)
                {
                    if (listRecipe[i].id == recipe.id)
                    {
                        listRecipe[i] = recipe;
                        break;
                    }
                }


                if (RecipeUpdateEvent != null)
                {
                    RecipeUpdateEvent();
                }
            }
        }

        public Recipe GetRecipe(int id, string name = null)
        {
            if (name == null)
            {
                name = AccountManager.instance.accountName;
            }
            List<Recipe> listRecipe;
            if (_mapName2recipes.TryGetValue(name, out listRecipe))
            {
                for (var i = 0; i < listRecipe.Count; i++)
                {
                    if (listRecipe[i].id == id)
                    {
                        return listRecipe[i];
                    }
                }
            }
            return null;
        }

        public List<Recipe> GetListRecipe(string name = null)
        {
            if (name == null)
            {
                name = AccountManager.instance.accountName;
            }
            List<Recipe> listRecipe;
            if (!_mapName2recipes.TryGetValue(name, out listRecipe))
            {
                listRecipe = new List<Recipe>();
                _mapName2recipes.Add(name, listRecipe);
            }
            return listRecipe;
        }
    }
}